#pragma once

#include "Common.h"
#include "stdafx.h"

struct delta
{
	sbyte dx, dy;

	delta(sbyte dx, sbyte dy)
		: dx(dx), dy(dy) { }
};

struct KnightMoves
{
	static const size_t RowSize = 9;

	byte Moves[NUM_SQUARES * RowSize];

	KnightMoves()
	{
		memset(Moves, 64, sizeof(Moves));

		sbyte xoffs [] = { -1, 1, -2, 2, -2, 2, -1, 1 };
		sbyte yoffs [] = { -2, -2, -1, -1, 1, 1, 2, 2 };

		for (sbyte i = 0; i < NUM_SQUARES; ++i)
		{
			byte *pCurr = &Moves[i * RowSize];

			sbyte cx = i & 0x7;
			sbyte cy = (i & ~0x7) >> 3;

			byte ct = 0;
			for (byte j = 0; j < 8; ++j)
			{
				sbyte x = cx + xoffs[j],
					y = cy + yoffs[j];

				if (x >= 0 && x < 8 && y >= 0 && y < 8)
				{
					pCurr[ct++] = y * 8 + x;
				}
			}
		}
	}
} s_possibleKnightMoves;

struct BishopMoves 
{
	static const size_t RowSize = 9 * 4;

	byte Moves[NUM_SQUARES * RowSize];


	BishopMoves()
	{
		memset(Moves, 64, sizeof(Moves));

		const delta deltas [] = { delta(-1, -1), delta(1, -1), delta(-1, 1), delta(1, 1) };

		for (sbyte sy = 0; sy < 8; ++sy)
		{
			for (sbyte sx = 0; sx < 8; ++sx)
			{
				sbyte srcIdx = sy * 8 + sx;

				byte *pCurr = Moves + srcIdx * RowSize;

				for (delta d : deltas)
				{
					byte ct = 1;
					for (sbyte dy = sy + d.dy, dx = sx + d.dx; 
						 dy >= 0 && dy < 8 &&
						 dx >= 0 && dx < 8; dy += d.dy, dx += d.dx)
					{
						pCurr[ct++] = dy * 8 + dx;
					}
					*pCurr = ct - 1;
					pCurr += ct;
				}

				
			}
		}
	}

} s_possibleBishopMoves;

struct KingMoves 
{
	static const size_t RowSize = 9;

	byte Moves[NUM_SQUARES * RowSize];

	KingMoves()
	{
		memset(Moves, 64, sizeof(Moves));

		for (sbyte sy = 0; sy < 8; ++sy)
		{
			for (sbyte sx = 0; sx < 8; ++sx)
			{
				sbyte srcIdx = sy * 8 + sx;

				byte *pCurr = Moves + srcIdx * RowSize;

				byte ct = 1;
				for (sbyte dy = sy - 1; dy <= sy + 1; ++dy)
				{
					for (sbyte dx = sx - 1; dx <= sx + 1; ++dx)
					{
						if (dx >= 0 && dx < 8 &&
							dy >= 0 && dy < 8)
						{
							if (!(dx == sx && dy == sy))
							{
								pCurr[ct++] = dy * 8 + dx;
							}
						}
					}
				}
				*pCurr = ct - 1;
			}
		}
	}

} s_possibleKingMoves;